SOURCE_DIR = ./{{cookiecutter.__project_slug}}

.PHONY: clean help

help:
	clear;
	@echo "================= Usage =================";
	@echo "clean                  : Remove autogenerated folders";
	@echo "clean-pyc              : Remove python artifacts."
	@echo "clean-build            : Remove build artifacts."
	@echo "install                : Install all dependencies and the package itself."
	@echo "bandit                 : Run bandit security analysis.";
	@echo "mypy                   : Run mypy type checking.";
	@echo "flake8                 : Run flake8 linting.";
	@echo "test                   : Run tests and generate coverage report.";
	@echo "build                  : Build a python wheel package.";
	@echo "publish                : Publish a python wheel package to package index.";

# Clean the folder from build/test related folders
clean: clean-build clean-pyc
	rm -rf .mypy_cache/ .pytest_cache/
	rm -f .coverage

clean-pyc:
	find . \( -name '*.pyc' -o -name '*.pyo' \) -exec rm -rf {} +

clean-build:
	rm -rf build/ dist/ *.egg-info

# Install development dependencies
install:
	poetry install --with dev,test,docs

# Install and run bandit security analysis
bandit:
	poetry run bandit -r $(SOURCE_DIR)

# Install and run mypy type checking
mypy:
	poetry run mypy $(SOURCE_DIR)

# Install and run flake8 linting
flake8:
	poetry run flake8 $(SOURCE_DIR)

# Install requirements for testing and run tests
test:
	poetry run pytest

# build wheel package
build:
	poetry build -f wheel

# publish the built package
publish:
	@if [ -n "${POETRY_PYPI_TOKEN_PYPI}" ]; then\
		echo "Uploading package to PyPi.";\
		poetry publish --build --skip-existing;\
	elif [ -n "${PACKAGE_INDEX_REPOSITORY_URL}" ] && [ -n "${PACKAGE_INDEX_USERNAME}" ] && [ -n "${PACKAGE_INDEX_PASSWORD}" ]; then\
		echo "Uploading package to private package index ${PACKAGE_INDEX_REPOSITORY_URL}.";\
		poetry config repositories.packagidx ${PACKAGE_INDEX_REPOSITORY_URL};\
		poetry config http-basic.packagidx "${PACKAGE_INDEX_USERNAME}" "${PACKAGE_INDEX_PASSWORD}";\
		poetry publish --build --skip-existing -r packagidx;\
	else\
		echo "To upload package to a private package index, you need to set environment variables \033[1mPACKAGE_INDEX_REPOSITORY_URL\033[0m \033[1mPACKAGE_INDEX_USERNAME\033[0m and \033[1mPACKAGE_INDEX_PASSWORD\033[0m.";\
		exit 1;\
	fi